package com.huatai.common.tool;

/**
 * @program: ht-system
 * @description: 类
 * @author: songxiude
 * @create: 2023-01-31 15:07
 **/


import com.huatai.common.tool.function.*;
import com.huatai.common.util.Exceptions;

import java.util.Comparator;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Unchecked {
    public Unchecked() {
    }

    public static <T, R> Function<T, R> function(CheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return (t) -> {
            try {
                return mapper.apply(t);
            } catch (Throwable var3) {
                throw Exceptions.unchecked(var3);
            }
        };
    }

    public static <T> Consumer<T> consumer(CheckedConsumer<T> mapper) {
        Objects.requireNonNull(mapper);
        return (t) -> {
            try {
                mapper.accept(t);
            } catch (Throwable var3) {
                throw Exceptions.unchecked(var3);
            }
        };
    }

    public static <T> Supplier<T> supplier(CheckedSupplier<T> mapper) {
        Objects.requireNonNull(mapper);
        return () -> {
            try {
                return mapper.get();
            } catch (Throwable var2) {
                throw Exceptions.unchecked(var2);
            }
        };
    }

    public static Runnable runnable(CheckedRunnable runnable) {
        Objects.requireNonNull(runnable);
        return () -> {
            try {
                runnable.run();
            } catch (Throwable var2) {
                throw Exceptions.unchecked(var2);
            }
        };
    }

    public static <T> Callable<T> callable(CheckedCallable<T> callable) {
        Objects.requireNonNull(callable);
        return () -> {
            try {
                return callable.call();
            } catch (Throwable var2) {
                throw Exceptions.unchecked(var2);
            }
        };
    }

    public static <T> Comparator<T> comparator(CheckedComparator<T> comparator) {
        Objects.requireNonNull(comparator);
        return (o1, o2) -> {
            try {
                return comparator.compare(o1, o2);
            } catch (Throwable var4) {
                throw Exceptions.unchecked(var4);
            }
        };
    }
}
